package com.fita.admin.service.agent.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.fita.admin.dao.AgentCompanyDao;
import com.fita.admin.dao.AgentDao;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.agent.AgentCompanyService;
import com.fita.entity.agent.AgentCompany;
import com.fita.entity.principal.Principal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: 11200
 * @date: 2019/6/19
 */
@Service("agentCompanyService")
public class AgentCompanyServiceImpl extends DataServiceImpl<AgentCompany> implements AgentCompanyService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Dao
    private AgentDao agentDao;

    @Dao
    private AgentCompanyDao agentCompanyDao;

    /**
     * 修改公司和公司下代理的销售
     *
     * @param agentCompanyId
     * @param principalId
     */
    public void changeSalesperson(Long agentCompanyId, Long principalId) {

        AgentCompany agentCompany = genericDaoSupport.load(AgentCompany.class, agentCompanyId);
        if (agentCompany == null){
            throw BusinessException.newInstance("500", "公司不存在");
        }
        Principal principal = genericDaoSupport.load(Principal.class, principalId);
        if (principal == null){
            throw BusinessException.newInstance("500", "销售不存在");
        }
        agentCompany.changeSalesperson(principal);
        genericDaoSupport.update(agentCompany);

        // 如果是一级代理，则其下二级代理同步修改销售
        if (agentCompany.getPrimaryAgent() == null){

            List<AgentCompany> agentCompanies = agentCompanyDao.listSecondaryAgentCompanyByPrimaryAgentCompany(agentCompany);

            agentCompanies.stream().forEach(secondaryAgentCompany -> {
                secondaryAgentCompany.changeSalesperson(principal);
                genericDaoSupport.update(secondaryAgentCompany);
            });

        }
    }

    /**
     * 计算一级代理的二级代理数量
     *
     * @return
     */
    public List calculateSecondaryAgentAmount() {

        List calculateSecondaryAgentAmount = agentCompanyDao.calculateSecondaryAgentAmount();

        return calculateSecondaryAgentAmount;
    }

    /**
     * 允许成为一级代理
     *
     * @param agentCompanyId
     */
    public void enablePrimaryAgent(Long agentCompanyId) {

        AgentCompany agentCompany = genericDaoSupport.load(AgentCompany.class, agentCompanyId);

        if (agentCompany.getPrimaryAgent() != null || agentCompany.getPrimaryAgentRate() != null){
            throw BusinessException.newInstance("500", "该公司为二级公司");
        }
        agentCompany.enablePrimayAgent();

        genericDaoSupport.update(agentCompany);
    }

    /**
     * 禁止成为一级代理
     *
     * @param agentCompanyId
     */
    public void disablePrimaryAgent(Long agentCompanyId) {

        AgentCompany agentCompany = genericDaoSupport.load(AgentCompany.class, agentCompanyId);

        if (agentCompany.getPrimaryAgent() != null || agentCompany.getPrimaryAgentRate() != null){
            throw BusinessException.newInstance("500", "该公司为二级公司");
        }
        agentCompany.disablePrimaryAgent();

        genericDaoSupport.update(agentCompany);

    }

    /**
     * 列出一级代理公司
     *
     * @return
     */
    public List<AgentCompany> listPrimaryAgentCompany() {

        List<AgentCompany> agentCompanies = agentCompanyDao.listAllPrimaryAgentCompany();

        return agentCompanies;
    }

}
